package org.jeecg.modules.hc.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.system.mapper.HcDailyOrderStatisticMapper;
import org.jeecg.modules.system.util.TimeFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class HcDailyOrderStatisticJob {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private HcDailyOrderStatisticMapper hcDailyOrderStatisticMapper;

//    @Scheduled(cron = "0/10 * * * * *")
    public void doSomething() {
        // 循环N次，计算最近N天的数据
        for (int i = 0; i < 2; i++) {
            calcDailyOrdersDistribution(i);
        }

        log.info("done");
    }

    private void calcDailyOrdersDistribution(int num) {
        log.info("begin to analyse order distribution");
        Calendar startTimeCalendar = Calendar.getInstance();
        startTimeCalendar.add(Calendar.DAY_OF_MONTH, -(num + 1));
//        startTimeCalendar.add(Calendar.DAY_OF_MONTH, -1);
        startTimeCalendar.set(Calendar.HOUR_OF_DAY, 21);
        startTimeCalendar.set(Calendar.MINUTE, 0);
        startTimeCalendar.set(Calendar.SECOND, 0);

        Calendar endTimeCalendar = Calendar.getInstance();
        endTimeCalendar.add(Calendar.DAY_OF_MONTH, -num);
//        endTimeCalendar.add(Calendar.DAY_OF_MONTH, 0);
        endTimeCalendar.set(Calendar.HOUR_OF_DAY, 21);
        endTimeCalendar.set(Calendar.MINUTE, 0);
        endTimeCalendar.set(Calendar.SECOND, 0);

        String startTime = TimeFormatUtil.format2YYYYMMddHHmmss(startTimeCalendar.getTime());
        String endTime = TimeFormatUtil.format2YYYYMMddHHmmss(endTimeCalendar.getTime());
        String yyyyMMdd = TimeFormatUtil.format2YYYYMMdd(endTimeCalendar.getTime());
        log.info("startTime: {}, endTime: {}, today redis key tag: {}", startTime, endTime, yyyyMMdd);
        List<HashMap<String, Object>> sites = hcDailyOrderStatisticMapper.queryAllSites();
        for (int i = 0; i < sites.size(); i++) {
            HashMap<String, Object> siteInfo = sites.get(i);
            String siteName = siteInfo.get("NAME").toString();
            log.info("site name: {},site id: {}", siteName, siteInfo.get("ID"));
            // 查询每个站点下的各个大厦
            List<HashMap<String, Object>> officeBuildings = hcDailyOrderStatisticMapper.queryOfficeBuildingBySiteId(siteInfo.get("ID").toString());
            // 遍历某个【站点】的【所有大厦】
            for (int k = 0; k < officeBuildings.size(); k++) {
                HashMap<String, Object> buildingInfo = officeBuildings.get(k);
                // 大厦id
                String buildingId = buildingInfo.get("ID").toString();
                // 大厦名称
                String buildingAddress = buildingInfo.get("ADDRESS_NAME").toString();
                List<Map<String, Object>> pickAddressCountMapList = hcDailyOrderStatisticMapper.queryDailyOrderCountByBuildingId(buildingId, startTime, endTime);
                for (int m = 0; m < pickAddressCountMapList.size(); m++) {
                    String pickAddress = (String) pickAddressCountMapList.get(m).get("pickAddress");
                    int orderCount = ((Long) pickAddressCountMapList.get(m).get("orderCount")).intValue();
                    // 完整的地址由三部分组成，站点_大厦名称_取餐楼层；在SortedSet中，每个元素的value存放完整地址，当查询时，通过 _ 进行字符串分割，然后传递给前端，显示在不同的列
                    String fullAddress = siteInfo.get("NAME") + "_" + buildingAddress + "_" + pickAddress;
                    log.info("building name: {},building id: {}, daily orders count: {}", buildingAddress, fullAddress, orderCount);

                    /** 不选站点，不选大厦时，从如下两个 kv对中取数据，两个的区别是，sortedset 中的是有序的，按照份数升序排列的： hash中存放的是乱序的，既不按照取餐楼层排序，也不按照份数排序 **/
                    // 不选【站点】时，从此 sortedset 中读取; 20230222::: -> [{站点名_大厦名_取餐楼层2,  8}， {站点名_大厦名_取餐楼层3,  11}, {站点名_大厦名_取餐楼层1,  16},]
                    redisUtil.zsetAdd("PickAddress_" + yyyyMMdd + ":::", orderCount, fullAddress);
                    // 不选【站点】时，从此 sortedset 中读取; 20230222::: -> [{站点名_大厦名_取餐楼层3,  11}， {站点名_大厦名_取餐楼层1,  16}, {站点名_大厦名_取餐楼层2,  8},]
                    redisUtil.hset("PickAddressMap_"+ yyyyMMdd + ":::", fullAddress, orderCount);

                    // 选某个【特定的站点】时，从此sortedset中读取; 20230222:站点:: -> [{大厦名_取餐楼层1, 9}, {大厦名_取餐楼层2, 11}, {大厦名_取餐楼层3, 13}]
                    redisUtil.zsetAdd("PickAddress_" + yyyyMMdd + ":" + siteName + "::", orderCount, buildingAddress + "_" + pickAddress);
                    // 选某个【特定的站点】时，从此sortedset中读取; 20230222:站点:: -> [{大厦名_取餐楼层2, 11}, {大厦名_取餐楼层1, 9}, {大厦名_取餐楼层3, 13}]
//                    redisUtil.hset("map"+ yyyyMMdd + ":" + siteName + "::", buildingAddress + "_" + pickAddress, orderCount);

                    // 选某个【特定的站点】下的【特定大厦】时，从此sortedset中读取; 20230222:站点:大厦名: -> [{取餐楼层2, 9}, {取餐楼层1, 11}, {取餐楼层3, 13}]
                    redisUtil.zsetAdd("PickAddress_" + yyyyMMdd + ":" + siteName + ":" + buildingAddress + ":", orderCount, pickAddress);
                    // 选某个【特定的站点】下的【特定大厦】时，从此sortedset中读取; 20230222:站点:大厦名: -> [{取餐楼层1, 11}, {取餐楼层3, 13}, {取餐楼层2, 9}]
//                    redisUtil.hset("map"+ yyyyMMdd + ":" + siteName + ":" + buildingAddress + ":", pickAddress, orderCount);
                }
                // 获取某个大厦的总订单数
                Integer totalCountByBuidling = hcDailyOrderStatisticMapper.queryDailyBuildingTotalOrderCount(buildingId, startTime, endTime);
                // AllBuilding_yyyyMMdd:::  -> [{站点名_大厦名， 11}]
                redisUtil.zsetAdd("Building_" + yyyyMMdd + ":::", totalCountByBuidling, siteInfo.get("NAME") + "_" + buildingAddress);
                redisUtil.hset("BuildingMap_"+ yyyyMMdd + ":::", siteInfo.get("NAME") + "_" + buildingAddress, totalCountByBuidling);
                // AllBuilding_yyyyMMdd:站点:: -> [{大厦名, 11}]
                redisUtil.zsetAdd("Building_" + yyyyMMdd + ":"+ siteName +"::", totalCountByBuidling, buildingAddress);
            }
            // select id from hc_site_address_details where SITE_ID  = '1622932345155772418'
            // select address_id, count(*) from hc_order_merchant where address_id in (xxxx, xxxx, xxxx, xxxx) group by address_id; 查询某个站点的所有大厦的订单，按照大厦名称进行分组
        }

        log.info("analyse order distribution done");
    }

}
